package com.luntan.action;

import java.util.Date;

import com.luntan.domain.HuiTie;
import com.luntan.domain.MainTie;
import com.luntan.domain.TUser;
import com.luntan.service.HuiTieService;
import com.luntan.service.MainTieService;
import com.luntan.util.web.Page;

/**
 * 回帖控制层
 * 
 * @author mashiqiong
 *
 */
public class HuiTieAction extends BaseAction {
	private Page<HuiTie> page = new Page<HuiTie>();
	private HuiTieService huiTieService;
	private MainTieService mainTieService;
	private HuiTie huiTie=new HuiTie(new TUser(),new MainTie());
	
	/**
	 * 添加页面
	 *
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		MainTie temp=mainTieService.getEntity(MainTie.class, huiTie.getMainTie().getMid());
		this.huiTie.setMainTie(temp);
		return INPUT;
	}
	
	/**
	 * 列表
	 *
	 * @return
	 * @throws Exception
	 */
	public String addSave() throws Exception {
		huiTie.setHdate(new Date());
		boolean flag=this.huiTieService.addSave(huiTie);
		if(flag){
			return "viewMainTie";
		}else{
			addFieldError("all", "保存失败");
			return INPUT;
		}
	}
	
	/**
	 * 验证添加的项是否符合要求
	 */
	public void validateAddSave() {
		common();
	}

	private void common() {
		
		if(this.huiTie.getHcontent()==null||"".equals(huiTie.getHcontent())){
			addFieldError("hcontent", "回帖子内容不能为空");
		}
	}
	
	/**
	 * 修改
	 *
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception {
		this.huiTie=this.huiTieService.getEntity(HuiTie.class, this.huiTie.getHid());
		return "update";
	}
	
	/**
	 * 保存修改
	 *
	 * @return
	 * @throws Exception
	 */
	public String updateSave() throws Exception {
		boolean flag=this.huiTieService.updateSave(huiTie);
		if(flag){
			return "viewMainTie";
		}else{
			addFieldError("all", "保存失败");
			return "update";
		}
	}
	
	/**
	 * 验证修改的项是否符合要求
	 */
	public void validateUpdateSave() {
		common();
	}
	
	/**
	 * 查看帖子
	 *
	 * @return
	 * @throws Exception
	 */
	public String view() throws Exception {
		this.huiTie=this.huiTieService.getEntity(HuiTie.class, this.huiTie.getHid());
		return "view";
	}
	
	
	/**
	 * 列表
	 *
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		page = new Page<HuiTie>();
		page.setResultlist(this.huiTieService.getList());

		return SUCCESS;
	}

	/**
	 * 删除帖子
	 *
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		boolean flag = this.huiTieService.delete(this.huiTie.getHid());
		if(!flag){
			addActionError("删除回帖失败");
		}
		
		return "viewMainTie";
	}
	
	public Page<HuiTie> getPage() {
		return page;
	}

	public void setPage(Page<HuiTie> page) {
		this.page = page;
	}

	public HuiTie getHuiTie() {
		return huiTie;
	}

	public void setHuiTie(HuiTie huiTie) {
		this.huiTie = huiTie;
	}

	public void setMainTieService(MainTieService mainTieService) {
		this.mainTieService = mainTieService;
	}
	
	public void sethuiTieService(HuiTieService huiTieService) {
		this.huiTieService = huiTieService;
	}

}
